<?php
//----------------------------------------------------------------------------//
// Class              : MD_DownloadDriver											  //
// Modification       :														  //
//----------------------------------------------------------------------------//
// Date(yyyy.mm.dd)   Developer                       Action				  //
//----------------------------------------------------------------------------//
// 2007.07.08         Oanhhtk                   Created					  //
//----------------------------------------------------------------------------//

class MD_DownloadDriver extends ModelBase{

	//==========================================================================
	// Function    :  MD_DownloadDriver
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk 2007.07.12
	// Details     : AppendData record of table
	//==========================================================================
	function MD_DownloadDriver(&$database){
		$this->tableName= TABLE_DOWNLOAD_DRIVER;
	   	ModelBase::ModelBase($database);
	}
	//==========================================================================
	// Function    :  insert
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk 2007.07.08
	// Details     : AppendData record of table
	//==========================================================================
	function insert(&$arrayData){
	    //Thay dau ' thanh '' khi insert du lieu vao Database
		$ten_danh_muc=$this->replaceString(&$arrayData["ten_danh_muc"]);
		
		//Insert du lieu vao DB
		$listFilds .= "ten_danh_muc,";		$listValues  .= "'".$ten_danh_muc."',";
		$listFilds .= "id_danh_muc,";		$listValues  .= "'".$arrayData['id_danh_muc_add']."',";
		$listFilds .= "ngay_dang,";    		$listValues  .= " now(),";
		$listFilds .= "ten_file";		   	$listValues  .= "'".$_FILES["userfile"]['name']."'";
		$sql = "INSERT INTO ".$this->tableName." (  $listFilds ) ";
		$sql .= " VALUES ($listValues ) ";
		//echo $sql;
		$this->db->query($sql);
	}
	//==========================================================================
	// Function    :  update
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk 2007.07.08
	// Details     : AppendData record of table
	//==========================================================================
	function update(&$arrayData)
	{
		//Thay dau ' thanh '' khi insert du lieu vao Database
		$ten_danh_muc=$this->replaceString(&$arrayData["ten_danh_muc"]);
		//Update du lieu vao DB
		$sql = "UPDATE  ".$this->tableName." SET ";
		$sql .= "ten_danh_muc='".$ten_danh_muc."',";
		$sql .= "id_danh_muc='".$arrayData['id_danh_muc_add']."',";
		if($_FILES["userfile"]['name']!=""){
			$sql .= "ten_file='".$_FILES["userfile"]['name']."'," ;
		}
		$sql .= "ngay_dang=now()";
		$sql .= " WHERE $this->keyField=".$arrayData["id"];
		//echo $sql;
		$this->db->query($sql);
	}
	//==========================================================================
    // Function    : update_ht
    // Creator     : Thanh 2007.07.13
    // Details     : update a record of table
    //==========================================================================
    function update_ht($strID, $strHienThi){
        $sql = "UPDATE  ".$this->tableName." SET ";
        if ($strHienThi == '1'){
            $sql .= "hien_thi='0'";                  
        } else {
            $sql .= "hien_thi='1'";                  
        }
        $sql .= " WHERE $this->keyField=".$strID;
        //echo $sql;
        $this->db->query($sql);
    }    
	//==========================================================================
	// Function    : getForSearchText
	// Creator     : ThanhTX 2007.06.08
	// Details     : Get all data from table
	// Update	   : Oanhhtk 2007.06.18  --> update using for admin	
	//==========================================================================
	function getForSearchText($searchText){
		$sql = " SELECT $this->listFieldSearch FROM $this->tableName";
		$sql .= " WHERE ";
		$sql .= "(tieu_de like '%".$searchText."%' or ";
		$sql .= " tom_tat like '%".$searchText."%' or ";
		$sql .= " noi_dung like '%".$searchText."%' or ";
		$sql .= " nguon_tin like '%".$searchText."%' or ";
		$sql .= " chu_thich_anh like '%".$searchText."%') ";
		$sql .= " and $this->tableName.hien_thi =1 ";
		$sql .= " order by $this->tableName.id desc";
		//echo $sql;
		return $this->db->retrieve($sql);
	}
	
	//==========================================================================
    // Function    : getContentNews("id,tieu_de",NUM_NEWREC);
    // Creator     : Thanhtx 2007.07.12
    // Details     : AppendData record of table
    //==========================================================================
    function getContentNews($strFields,$intNum_NewRec){
        if ($strFields != ""){
            $sql = "SELECT  ".$strFields." FROM ".$this->tableName." WHERE hien_thi = 1 ORDER BY id desc LIMIT $intNum_NewRec;";
        } else {
            $sql = "SELECT * FROM ".$this->tableName." WHERE hien_thi = 1 ORDER BY id desc LIMIT $intNum_NewRec;";
        }
        //echo $sql;
        return $this->db->retrieve($sql);
    }
	//==========================================================================
    // Function    : getContentNew("id,tieu_de",NUM_NEWREC);
    // Creator     : Thanhtx 2007.07.12
    // Details     : AppendData record of table
    //==========================================================================
    function getContentNew($strFields,$intNum_NewRec){
        if ($strFields != ""){
            $sql = "SELECT  ".$strFields." FROM ".$this->tableName." WHERE hien_thi = 1 ORDER BY id desc LIMIT $intNum_NewRec;";
        } else {
            $sql = "SELECT * FROM ".$this->tableName." WHERE hien_thi = 1 ORDER BY id desc LIMIT $intNum_NewRec;";
        }
        //echo $sql;
        return $this->db->retrieve($sql);
    }
	//==========================================================================
    // Function    : getContentFocus("id,tieu_de",NUM_NEWREC);
    // Creator     : Thanhtx 2007.07.12
    // Details     : AppendData record of table
    //==========================================================================
    function getContentFocus($strFields,$intNum_NewRec){
        if ($strFields != ""){
            $sql = "SELECT  ".$strFields." FROM ".$this->tableName." WHERE hien_thi = 1 ORDER BY id desc LIMIT $intNum_NewRec;";
        } else {
            $sql = "SELECT * FROM ".$this->tableName." WHERE hien_thi = 1 ORDER BY id desc LIMIT $intNum_NewRec;";
        }
        //echo $sql;
        return $this->db->retrieve($sql);
    }
	//==========================================================================
	// Function    :  update
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk 2007.07.12
	// Details     : AppendData record of table
	//==========================================================================
	function updateCountClick(&$id)
	{
		$sql = "UPDATE  ".$this->tableName." SET ";
		$sql .= "hits=hits+1";
		$sql .= " WHERE $this->keyField=".$id;
		$this->db->query($sql);
	}
	//==========================================================================
	// Function    :  updateanh
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk 2007.03.08
	// Details     : AppendData record of table
	//==========================================================================
	function updateanh(&$arrayData){
		$sql = "UPDATE  ".$this->tableName." SET ";
		$sql .= "anh=''" ;	
		$sql .= " WHERE $this->keyField=".$arrayData["id"];
		$this->db->query($sql);
	}
	//==========================================================================
	// Function    : getFullById
	// Argument    : 
	// Creator     : ThanhTX 2007.06.11
	// Details     : Get record with id
	//==========================================================================
	function getFullById($id){
		$sql = "SELECT * FROM $this->tableName ";
		$sql .= "  WHERE id=$id";
		//echo $sql;
		return $this->db->getFirstRecord($sql);
	}
	//==========================================================================
	// Function    : getByWhereLimit20
	// Argument    :
	// ReturnValue :
	// Creator     : Oanhhtk 2007.03.09
	// Details     : AppendData record of table
	//==========================================================================
	function getByWhereLimit20($listField,$where="",$order=""){
		if ($where==""){
			$sql = "SELECT $listField  FROM $this->tableName  ";
		}else{
  			$sql = "SELECT $listField  FROM $this->tableName  WHERE $where ";
		}
		if ($order !=""){
			$sql .= " Order by $order";
		}
		if ($order !=""){
			$sql .= " limit 1,20";
		}
		//global $db;
		return $this->db->retrieve($sql);
	}
	//==========================================================================
	// Function    : getForSearchTextAdmin
	// Creator     : ThanhTX 2007.06.08
	// Details     : Get all data from table
	// Update	   : Oanhhtk 2007.06.18  --> update using for admin	
	//==========================================================================
	function getForSearchTextAdmin($searchText,$cat_id,$section_id,$hien_thi){
		$sql = "SELECT $this->tableName.* FROM $this->tableName";
		$sql .= "  WHERE ";
		$sql .= "(title like '%".$searchText."%' or ";
		$sql .= "introtext like '%".$searchText."%' or ";
		$sql .= "fulltexts like '%".$searchText."%') and ";
		$sql .= "(1=1 ";
		if($cat_id!=""){
			$sql .= "and cat_id =".$cat_id."  ";
		}
		if($section_id!=""){
			$sql .= "and section_id =".$section_id." ";
		}
		if($hien_thi!=""){
			$sql .= "and hien_thi=".$hien_thi." ";
		}
		$sql .= ")";
		$sql .= " order by $this->tableName.id desc";
		//echo $sql;
		return $this->db->retrieve($sql);
	}
}
?>